//
//  QSCardCycleViewCell.m
//  MyTest
//
//  Created by Song on 2017/9/12.
//  Copyright © 2017年 Song. All rights reserved.
//

#import "QSCardCycleViewCell.h"
#import <UIImageView+WebCache.h>
#import <Masonry.h>

@interface QSCardCycleViewCell()
@property (nonatomic, weak) UIImageView *imageView;
@end

@implementation QSCardCycleViewCell
/**
 *  重写initWithFrame:方法
 */
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupImageView];
    }
    return self;
}

/**
 *  设置imageView
 */
- (void)setupImageView{
    UIImageView *imageView = [[UIImageView alloc] init];
    [imageView setUserInteractionEnabled:YES];
    [self.contentView addSubview:imageView];
    _imageView = imageView;
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0.0);
    }];
}

/**
 *  重写imageName的set方法
 */
- (void)setImageName:(id)imageName
{
    if ([imageName isKindOfClass:[NSString class]]) {
        if (!kStringIsEmpty(imageName)) {
            __weak typeof(self) weakSelf = self;
            
            NSString *imgName = nil;
            if ([imageName hasPrefix:@"http"] || [imageName hasPrefix:@"https"]) {
                imgName = imageName;
            } else {
                imgName = [kInterfaceAddress stringByAppendingString:imageName];
            }
            
            SDWebImageManager *mgr = [SDWebImageManager sharedManager];
            [[mgr imageCache] queryDiskCacheForKey:imgName done:^(UIImage *image, SDImageCacheType cacheType) {
                
                if(image){
                    [_imageView setImage:image];
                }else{
                    [_imageView sd_setImageWithURL:[NSURL URLWithString:imgName] placeholderImage:[UIImage imageNamed:weakSelf.placeHolderImg] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        if(!error){
                        }
                    }];
                }
            }];
        }
    } else {
        [_imageView setImage:imageName];
    }
}

- (void)setCornerAngle:(CGFloat)cornerAngle
{
    _cornerAngle = cornerAngle;
    self.imageView.layer.cornerRadius = cornerAngle;
    self.imageView.layer.masksToBounds = YES;
}
@end
